// components/ly/ly.js
let gecihight=0
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    islyshow:{
      type:Boolean,
      value:false
    },
    lys:String
  },
  observers:{
    lys(lrc){
      if(lrc =="暂无歌词"){
        this.setData({
          lyslist:[{
            time:0,
            lrc
          }],
          nowindex:-1
        })
      }else{
        this._parsely(lrc)
      }
      
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    scroll:0,
    lyslist:[],
    nowindex:0//当前索引歌词
  },
  lifetimes:{
    ready(){
      wx.getSystemInfo({
        success: function(res) {
        gecihight=  res.screenWidth / 750 *64
        },
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    update(currtime){
      let lyslist=this.data.lyslist
      if(lyslist.length==0){
        return
      }
      if(currtime>lyslist[lyslist.length-1].time){
        if(this.data.nowindex !=-1){
          this.setData({
            nowindex: -1,
            scroll:lyslist.length*gecihight
          })
        }
      }
      for (let i=0,len=lyslist.length;i<len;i++){
        if(currtime <=lyslist[i].time){
          this.setData({
            nowindex:i-1,
            scroll:(i-1)*gecihight
          })
          break
        }
      }
        
     
    },
    _parsely(slys){
      let line =slys.split('\n')
      let _lyslist=[]
      line.forEach((item)=>{
        let time = item.match(/\[(\d{2,}):(\d{2})(?:\.(\d{2,3}))?]/g)
        if(time !=null){
          let lrc=item.split(time)[1]
          let timeReg =time[0].match(/(\d{2,}):(\d{2})(?:\.(\d{2,3}))?/)
          //获取的时间时00：00：00
          let time2Seconds = parseInt(timeReg[1]) * 60 + parseInt(timeReg[2]) + parseInt(timeReg[3]) / 1000 
          _lyslist.push({
            lrc,
            time: time2Seconds,
          })
        }
      })
      this.setData({
        lyslist:_lyslist
      })
    }
  }
})
